package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
//import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);

        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Orders> pageInfo = new Page(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Orders::getUserName,name);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,dtoPage,"records");


        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map(item -> {
            OrdersDto orderDetail = new OrdersDto();

            BeanUtils.copyProperties(item,orderDetail);
            Long userId = item.getUserId();//    item.getid
            Orders orders = orderService.getById(userId);
          //  orderDetail.setUserId(userId);
            if (orders != null) {
                String ordersUserName = orders.getUserName();
                orderDetail.setUserName(ordersUserName);
                //setmealDto.setCategoryName(categoryName);
            }return orderDetail;

        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @GetMapping("/userPage")
    public R<Page> newOrder(int page,int pageSize){
        Page<Orders> page1 = new Page(page,pageSize);
        Page<OrdersDto> page2 = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //queryWrapper.last("limit 1");
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(page1,queryWrapper);
//        Orders one = orderService.getOne(queryWrapper);
        LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();

     //  BeanUtils.copyProperties(page1,page2,"records");
        List<Orders> records = page1.getRecords();

        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            Long id = record.getId();
            List<OrderDetail> orderDetailList = getOrderDetailListByOrderId(id);
            BeanUtils.copyProperties(record,ordersDto);
            ordersDto.setOrderDetails(orderDetailList);
           // return ordersDto;
            ordersDtos.add(ordersDto);

            /*OrderDetail byId = orderDetailService.getById(id);
            ArrayList<OrderDetail> orderDetails = new ArrayList<>();
            orderDetails.add(byId);
            ordersDto.setOrderDetails(orderDetails);
*/
        }
        BeanUtils.copyProperties(page1,page2,"records");
       page2.setRecords(ordersDtos);
        return R.success(page2);

    }

    public List<OrderDetail> getOrderDetailListByOrderId(Long id) {
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> list = orderDetailService.list(queryWrapper);
        return list;
    }
}